The Fundamentals of CC++ Game Programming: Using Target-based Development on SBC's by Brian Beuken

The Fundamentals of CC++ Game Programming: Using Target-based Development on SBC's by Brian Beuken

Author:Brian Beuken [Beuken, Brian]
Language: eng
Format: epub
Publisher: CRC Press
Published: 2018-02-21T00:00:00+00:00


A New Dawn for Particle Kind!

Our current Particle is really a hot decaying Particle and we shouldn’t really have its speeds for motion set up in the constructor, that’s more the emitters job, but the key function of time and knowing when to die are what makes it useful, in fact that’s kinda the only logic we care about here that makes it a particle. The individual behavior of the particle will vary so we should create new classes that process the behavior and then let the Particle Class update function, process the decay, and doom of our short but beautiful life.

Create a new header called HotDecay.h and maybe put it in a new filter on your solution, because you might make a lot of these, I’ve made a filter called Particles. The files themselves can still live in the Header and Source directories, but once these simple things are made we might want to fold away the filter and forget about these files later. HotDecay.h should contain this:

// a simple hot particle that is affected by gravity

#pragma once

#include "Objects.h"

#include "MyFiles.h"

#include "Particle.h"

class HotDecay : public Particle

{

public:

 HotDecay(); // normal is 1 × 1 surface

  ~HotDecay();

 void Update(Surface* s); // of course we need our update

};

As you can see, just a simple constructor/destructor pair and an update system, since I am inheriting from Particle, it will have the Time value, and since Particle inherits from Objects, all the other useful data are in there. This time I have not left a constructor for a file in here, since I’m quite sure that on this occasion I’m not going to use it.

Make a new HotDecay.cpp file, again perhaps by adding a filter in your solution, and add this code:

#include "HotDecay.h"

HotDecay::HotDecay()

{

// we can set these up here a defaults but the emmiter should change them

 Xpos = Ypos = Xspeed = Yspeed = 0; // clear these

 this->MarkForRemoval = false;

 this->Image = new Surface(Rand(2) + 1, Rand(2) + 1); // make a small surface slightly different sizes

 this->Image->ClearBuffer(Colour); // create a blue dot

 Time = 4.0f;

 Xspeed = 3.0f - Rand(6.0f);

 Yspeed = 3.0f - Rand(6.0f);

}

HotDecay::~HotDecay(){}

void HotDecay::Update(Surface* mScreen)

{

 Particle::Update(mScreen);

 // colour is a bit tricky

 unsigned int r = (this->Colour & REDMASK);

 unsigned int g = (Colour & GREENMASK) >> 8;

 unsigned int b = (Colour & BLUEMASK) >> 16;

 r -= 2;

 if (r > 256) r = 0;

 g -= 5;

 if (g > 256) g = 0;

 b -= 5;

 if (b > 256) b = 0;

 Colour = r + (g << 8) + (b << 16) + 0xff000000;

 if (Colour == ALPHAMASK) this->MarkForRemoval = true; // it’s dead so kill it

 Image->ClearBuffer(Colour);

 Image->CopyTo(mScreen, (int)Xpos, (int)Ypos);

 // finally assume they are affected by gravity and reduce the Ypeed

 Yspeed += 9.81f / 100; // use a real world value but scale to suit.

// give it a terminal vel

 if (Yspeed > 10.0f) Yspeed = 10.0f;

// and dampen the xseed

  Xspeed *= 0.99f; / reduces it slowly

}

It should be pretty clear that this is the old Particle code itself, now with the logic separated into this class. The Particle.cpp update routine has had this code removed, and now looks



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.